/**
 *Main Window: Display the list of contacts
 * @author Martin LEGRIS
 */
package View;

import View.Window;
import Model.Contact;
import adressbook.AdressBook;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;



public class Window extends JFrame implements ActionListener {

    private JPanel buttons_pan = new JPanel();
    private JPanel contact_list = new JPanel();
    private JPanel container = new JPanel();
    private JButton newContact = new JButton();
    private JButton deleteContact = new JButton();
    private JButton refreshList = new JButton();
    private JButton openContact = new JButton();
    private JButton byFName = new JButton();
    private JButton byLName = new JButton();
    private JButton byAge = new JButton();
    ArrayList<Contact> localContacts = new ArrayList<>();

    private JList<String> displayList = new JList<String>();

    public Window(ArrayList<Contact> myContacts) {
        //Create and set up the window.
        this.setTitle("Address Book");
        //this.setSize(100, 500);
        this.setLocationRelativeTo(null); //on positionne la fenetre au centre
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(container);

        //JPanel buttons
        buttons_pan.setLayout(new GridLayout(4, 1));
        //Définition de sa couleur de fond
        buttons_pan.setBackground(Color.LIGHT_GRAY);
        container.add(buttons_pan);

        //JPanel liste contact
        this.refresh();
        container.add(contact_list);
        contact_list.add(displayList);

        //JButton   
        newContact.setText("New Contact");
        buttons_pan.add(newContact);
        newContact.addActionListener(this);
        deleteContact.setText("Delete");
        deleteContact.addActionListener(this);
        buttons_pan.add(deleteContact);
        openContact.setText("Open");
        buttons_pan.add(openContact);
        openContact.addActionListener(this);
        refreshList.setText("RefreshList");
        buttons_pan.add(refreshList);
        refreshList.addActionListener(this);
        /*byFName.setText("Order by First Name");
        buttons_pan.add(byFName);
        byFName.addActionListener(this);;
        byLName.setText("Order by Last Name");
        buttons_pan.add(byLName);
        byLName.addActionListener(this);
        byAge.setText("Order by age");
        buttons_pan.add(byAge);
        byAge.addActionListener(this);*/

        //Construction of the window
        container.add(buttons_pan);
        container.add(contact_list);

        //Display the window.
        this.pack();
        this.setVisible(true);
    }

    public void refresh() {

        String[] content = new String[AdressBook.getMyContacts().size()];
        for (int i = 0; i < AdressBook.getMyContacts().size(); i++) {
            content[i] = AdressBook.getMyContacts().get(i).toString();
        }

        displayList.setListData(content);

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        //Lorsque l'on clique sur le bouton, on met à jour le JLabel
        if (arg0.getSource() == newContact) {
            try {
                NewContactWindow lafenetre = new NewContactWindow();
            } catch (IOException ex) {
                Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
            }
            refresh();

        }
        if (arg0.getSource() == openContact) {
            for (Object selectedValue : displayList.getSelectedIndices()) {
                try {
                    AdressBook.displayContact((int) selectedValue);
                } catch (IOException ex) {
                    Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        if (arg0.getSource() == refreshList) {
            refresh();

        }
        if (arg0.getSource() == deleteContact) {
            for (Object selectedValue : displayList.getSelectedIndices()) {
                AdressBook.deleteContact((int) selectedValue);
                refresh();
            }
        }
        if (arg0.getSource() == byFName) {
            
        }

    }
}
